package cc.mrbird.febs.business.controller;

import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.business.entity.BusinessModule;
import cc.mrbird.febs.business.service.IBusinessModuleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.Map;

@Slf4j
@Validated
@Controller
@RequestMapping(value = "/businessModule")
public class BusinessModuleController extends BaseController {

    @Resource
    private IBusinessModuleService businessModuleService;

    @GetMapping("/list")
    @ResponseBody
    public FebsResponse list(QueryRequest request, BusinessModule businessModule) {
        Map<String, Object> dataTable = getDataTable(businessModuleService.query(request, businessModule));
        return new FebsResponse().success().data(dataTable);
    }

    @PostMapping("/add")
    @ResponseBody
    @RequiresPermissions("businessModule:add")
    public FebsResponse add(BusinessModule businessModule) {
        if (businessModuleService.add(businessModule)) {
            return new FebsResponse().success();
        } else {
            return new FebsResponse().fail().message("添加失败，存在同名业务模块!");
        }
    }

    @PostMapping("/update")
    @ResponseBody
    @RequiresPermissions("businessModule:update")
    public FebsResponse update(BusinessModule businessModule) {
        if (businessModuleService.update(businessModule)) {
            return new FebsResponse().success();
        } else {
            return new FebsResponse().fail().message("修改失败，存在同名业务模块!");
        }
    }

    @GetMapping("/remove/{ids}")
    @ResponseBody
    @RequiresPermissions("businessModule:remove")
    public FebsResponse remove(@NotBlank(message = "{required}") @PathVariable String ids) {
        if (businessModuleService.remove(ids)) {
            return new FebsResponse().success();
        } else {
            return new FebsResponse().fail().message("删除失败,选择的业务存在子业务!");
        }
    }

    @PostMapping("/listAll")
    @ResponseBody
    public FebsResponse listAll() {
        return new FebsResponse().success().data(businessModuleService.listAll());
    }
}
